package com.application.sistercityapplication.news;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.sistercityapplication.dto.NewsDto;
import com.application.sistercityapplication.main.R;
import com.application.sistercityapplication.utils.DateUtil;
import com.squareup.picasso.Picasso;

@SuppressLint({ "SetJavaScriptEnabled", "ValidFragment" })
public class FullNewsMainActivity extends FragmentActivity {
	private ImageView closeButt;
	private TextView country_name, news_date, news_header, news_contents;
	PlaceSlidesFragmentAdapter mAdapter;
	
	private ViewPager mPager;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_full_news_main);
		closeButt = (ImageView) findViewById(R.id.imageView2);
		country_name = (TextView) findViewById(R.id.country_name);
		news_date = (TextView) findViewById(R.id.news_date);
		news_header = (TextView) findViewById(R.id.news_header);
		news_contents = (TextView) findViewById(R.id.news_contents);
		NewsDto model = (NewsDto) getIntent().getSerializableExtra("news");
		List<String> imgPath = new ArrayList<String>();
		try {
			country_name.setText(model.getCountry_name());
			news_date.setText(DateUtil.convertStringToStringFormat(model
					.getUpdated_at()));
			news_header.setText(model.getTitle());
			news_contents.setText(model.getDescription());
			if (model.getImage_path() != null) {
				//Test sliding image view pager
				imgPath.add(model.getImage_path());
				imgPath.add(model.getImage_path());
				imgPath.add(model.getImage_path());
			} else {
				imgPath = model.getGallery();
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		mAdapter = new PlaceSlidesFragmentAdapter(getSupportFragmentManager(),imgPath);
		mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
		closeButt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FullNewsMainActivity.this.finish();
			}
		});

	}

	public class PlaceSlidesFragmentAdapter extends FragmentPagerAdapter {
		List<String> _imgPath ;

		public PlaceSlidesFragmentAdapter(FragmentManager fm,List<String> imgPath) {
			super(fm);
			_imgPath = imgPath;
		}

		@Override
		public Fragment getItem(int position) {
			return new PlaceSlideFragment(_imgPath.get(position));
		}

		@Override
		public int getCount() {
			return _imgPath.size();
		}
	}

	public final class PlaceSlideFragment extends Fragment {
		String imageResourceId;

		public PlaceSlideFragment(String i) {
			imageResourceId = i;
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			
			View v = inflater.inflate(R.layout.image_news, null);
			
			ImageView imv = (ImageView) v.findViewById(R.id.news_image_layout);
			Picasso.with(FullNewsMainActivity.this).load(imageResourceId).into(imv);
			

			return v;
		}
	}
}
